//
//  UIBarButtonItem+Extension.swift
//  Weibo
//
//  Created by xu on 2017/3/18.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

//给BarButton扩展构造方法
extension UIBarButtonItem {
    
    convenience init(title:String = "", imageName: String? = nil ,target:Any? ,action:Selector?) {
        //实例化按钮
        let btn = UIButton ()
    
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        if let img = imageName {
            
            //添加图片
            btn.setImage(UIImage(named:img), for:.normal)
            btn.setImage(UIImage(named:img + "_highlighted"), for: .highlighted)

        }
        
        //监听按钮的点击事件
        if let ac = action {
            btn.addTarget(target, action: ac, for: .touchUpInside)
        }
       
        //自适应大小
        btn.sizeToFit()
        
        self.init()
        
        //设置右边导航栏的按钮
      
        self.customView = btn

    }
}



////MARK: sd Image 进行进一步的封装

